﻿//  --------------------------------
//  Copyright (c) AW Systems, Inc. All rights reserved.
//  This source code is made available under the terms of the Microsoft Public License (Ms-PL)
//  http://www.codeplex.com/SLAM/license
//  ---------------------------------

using System;
using System.Web;
using Slam.Activation;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Slam.Administration
{
	public partial class Activation : LayoutsPageBase
	{
		private int timeout;
		private string slamFolder = "";
		protected override void OnInit(EventArgs e)
		{
			timeout = Server.ScriptTimeout;
			Server.ScriptTimeout = 8000;
			base.OnInit(e);
		}

		protected override void OnUnload(EventArgs e)
		{
			try
			{
				Server.ScriptTimeout = timeout;
			}
			catch { }
			base.OnUnload(e);
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			//if under a site collection other than the root, doing a simple
			//redirect to the page will always redirect to the root, so the desired
			//location needs extracted from the RawUrl
			slamFolder = Request.RawUrl.Substring(0, Request.RawUrl.LastIndexOf("/") + 1);

			if (!IsPostBack)
			{
				//Test if SLAM can clear instances, if it cannot go back to the Control Panel
				try
				{
					ComponentService.ClearInstances();
				}
				catch (Exception ex)
				{
					Response.Redirect(slamFolder + "SLAMControlPanel.aspx");
				}
			}
			else
			{
				ComponentService.ClearInstances();
			}
		}

		/// <summary>
		/// Event handler for synchronizing SLAM
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void btnSynchronize_Click(object sender, EventArgs e)
		{
			Response.Redirect(slamFolder + "Synchronization.aspx");
		}

		/// <summary>
		/// Event handler for reactivating SLAM
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void btnAttachEvents_Click(object sender, EventArgs e)
		{
			IActivator activator = ComponentService.GetInstance<IActivator>();
			SPWeb currentSite = SPControl.GetContextWeb(HttpContext.Current);
			if (currentSite.Properties["SLAMLastActivationDate"] != null)
			{
				activator.Deactivate(currentSite.Url);
				currentSite.Properties["SLAMLastActivationDate"] = DateTime.Now.ToString();
				currentSite.Properties.Update();
			}
			else
			{
				currentSite.Properties.Add("SLAMLastActivationDate", DateTime.Now.ToString());
				currentSite.Properties.Update();
			}

			activator.Activate(currentSite.Url);
			ComponentService.ClearInstances();
			Response.Redirect(slamFolder + "Diagnostics.aspx");
		}

		/// <summary>
		/// Event handler for deactivating SLAM
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void btnDetachEvents_Click(object sender, EventArgs e)
		{
			IActivator activator = ComponentService.GetInstance<IActivator>();
			SPWeb currentSite = SPControl.GetContextWeb(HttpContext.Current);
			activator.Deactivate(currentSite.Url);
			ComponentService.ClearInstances();
			Response.Redirect(slamFolder + "Diagnostics.aspx");
		}

		protected void btnBack_Click(object sender, EventArgs e)
		{
			Response.Redirect(slamFolder + "SLAMControlPanel.aspx");
		}
	}
}
